package com.cryptoneedle.center.doris.service.doris;

import cn.hutool.v7.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.bubbles.engine.data.core.repository.BaseRepository;
import com.bubbles.engine.data.core.service.BaseService;
import com.cryptoneedle.center.common.entity.doris.DorisTable;
import com.cryptoneedle.center.common.key.TableKey;
import com.cryptoneedle.center.common.repository.doris.DorisTableRepository;
import com.cryptoneedle.center.doris.domain.metadata.DorisMetadataTable;
import com.cryptoneedle.center.doris.repository.metadata.DorisMetadataRepository;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>description: Doris Table </p>
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Service
@DS("pg")
@Slf4j
public class DorisTableService extends BaseService<DorisTable, TableKey> {

    @Resource
    private DorisTableService dorisTableService;

    @Resource
    private DorisTableRepository dorisTableRepository;

    @Resource
    private DorisMetadataRepository dorisMetadataRepository;

    @Override
    public BaseRepository<DorisTable, TableKey> getRepository() {
        return this.dorisTableRepository;
    }


    @Transactional
    public List<DorisTable> list() {
        return dorisTableRepository.list();
    }

    @Transactional
    public List<DorisTable> listByDatabase(String catalogName, String databaseName) {
        return dorisTableRepository.listByDatabase(catalogName, databaseName);
    }

    @Transactional
    public void addList(List<DorisTable> list) {
        dorisTableRepository.saveAll(list);
    }

    @Transactional
    public void saveList(List<DorisTable> list) {
        dorisTableRepository.saveAll(list);
    }

    @Transactional
    public void deleteList(List<DorisTable> list) {
        dorisTableRepository.deleteAll(list);
    }

    public List<DorisTable> syncDorisTable(String catalogName, String databaseName) {
        // 查询所有
        List<DorisTable> originList = dorisTableService.listByDatabase(catalogName, databaseName);
        Map<String, DorisTable> originMap = Maps.uniqueIndex(originList, DorisTable::getKeyString);

        // 待同步数据
        List<DorisTable> dealList = dorisMetadataRepository.showTableStatusFrom(catalogName, databaseName)
                .stream()
                .map(deal -> BeanUtil.copyProperties(deal, DorisTable.class))
                .toList();
        Map<String, DorisTable> dealMap = Maps.uniqueIndex(dealList, DorisTable::getKeyString);
        // 补充数据
        List<DorisMetadataTable> dealEnhanceList = dorisMetadataRepository.showFullTablesFrom(catalogName, databaseName);
        Map<String, DorisMetadataTable> dealEnhance = Maps.uniqueIndex(dealEnhanceList, DorisMetadataTable::getKey);
        dealList.forEach(deal -> {
            DorisMetadataTable enhance = dealEnhance.get(deal.getKeyString());
            if (enhance != null) {
                deal.setTableType(enhance.getTableType());
                deal.setStorageFormat(enhance.getStorageFormat());
                deal.setInvertedIndexStorageFormat(enhance.getInvertedIndexStorageFormat());
            }
        });

        // 新增数据
        List<DorisTable> addList = dealList.stream().filter(deal -> !originMap.containsKey(deal.getKeyString())).toList();

        // 移除数据
        List<DorisTable> removeList = originList.stream().filter(item -> !dealMap.containsKey(item.getKeyString())).toList();

        // 保存数据
        List<DorisTable> saveList = dealList
                .stream()
                .filter(deal -> originMap.containsKey(deal.getKeyString()))
                .peek(deal -> {
                    DorisTable origin = originMap.get(deal.getKeyString());
                }).toList();

        dorisTableService.addList(addList);
        dorisTableService.saveList(saveList);
        dorisTableService.deleteList(removeList);

        return dorisTableService.listByDatabase(catalogName, databaseName);
    }
}